Use deep_merge instead of merge to allow configuring Agent.options within a nested hash. Useful when configuring DataOutputAgent template>item options with CommanderAgent. Also fix SchedulerAgent 'check' typo.

Brian Petro 8 years ago
parent
commit
87a58827ff

+ 2 - 2
app/concerns/agent_controller_concern.rb

@@ -7,7 +7,7 @@ module AgentControllerConcern
7 7
 
8 8
   def default_options
9 9
     {
10
-      'action' => 'run',
10
+      'action' => 'run'
11 11
     }
12 12
   end
13 13
 
@@ -68,7 +68,7 @@ module AgentControllerConcern
68 68
             log "Agent '#{target.name}' is disabled"
69 69
           end
70 70
         when 'configure'
71
-          target.update!(options: target.options.merge(interpolated['configure_options']))
71
+          target.update! options: target.options.deep_merge(interpolated['configure_options'])
72 72
           log "Agent '#{target.name}' is configured with #{interpolated['configure_options'].inspect}"
73 73
         when ''
74 74
           # Do nothing

+ 1 - 1
app/models/agents/scheduler_agent.rb

@@ -87,7 +87,7 @@ module Agents
87 87
       true
88 88
     end
89 89
 
90
-    def check!
90
+    def check
91 91
       control!
92 92
     end
93 93
 

+ 21 - 0
spec/fixtures/agents.yml

@@ -111,3 +111,24 @@ jane_basecamp_agent:
111 111
   user: jane
112 112
   service: generic
113 113
   guid: <%= SecureRandom.hex %>
114
+
115
+
116
+bob_data_output_agent:
117
+  type: Agents::DataOutputAgent
118
+  user: bob
119
+  name: RSS Feed 
120
+  guid: <%= SecureRandom.hex %>
121
+  options: <%= {
122
+    expected_receive_period_in_days: 3,
123
+    secrets: ['secret'],
124
+    template: {
125
+      title: 'unchanged',
126
+      description: 'unchanged',
127
+      item: {
128
+        title: 'unchanged',
129
+        description: 'unchanged',
130
+        author: 'unchanged',
131
+        link: 'http://example.com'
132
+        }
133
+      }
134
+    }.to_json.inspect %>

+ 1 - 1
spec/models/agents/scheduler_agent_spec.rb

@@ -88,7 +88,7 @@ describe Agents::SchedulerAgent do
88 88
   describe "check!" do
89 89
     it "should control targets" do
90 90
       stub(agent).control!.once { nil }
91
-      agent.check!
91
+      agent.check
92 92
     end
93 93
   end
94 94
 end

+ 19 - 0
spec/support/shared_examples/agent_controller_concern.rb

@@ -128,5 +128,24 @@ shared_examples_for AgentControllerConcern do
128 128
       expect(agent.control_targets.reload).to all(satisfy { |a| a.options['url'] == 'http://some-new-url.com/SOMETHING' })
129 129
       expect(agents(:bob_website_agent).reload.options).to eq(old_options.merge('url' => 'http://some-new-url.com/SOMETHING'))
130 130
     end
131
+
132
+    it "should configure targets with nested objects" do
133
+      agent.control_targets << agents(:bob_data_output_agent)
134
+      agent.options['action'] = 'configure'
135
+      agent.options['configure_options'] = { 
136
+        template: {
137
+          item: {
138
+           title: "changed"
139
+          }
140
+        }
141
+      }
142
+      agent.save!
143
+      old_options = agents(:bob_data_output_agent).options
144
+
145
+      agent.check
146
+
147
+      expect(agent.control_targets.reload).to all(satisfy { |a| a.options['template'] && a.options['template']['item'] && (a.options['template']['item']['title'] == 'changed') })
148
+      expect(agents(:bob_data_output_agent).reload.options).to eq(old_options.deep_merge(agent.options['configure_options']))
149
+    end
131 150
   end
132 151
 end